package com.atwork.wuhua.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.MainPageAdapter;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.ui.fragment.CommunityFragment;
import com.atwork.wuhua.ui.fragment.HomePageFragment;
import com.atwork.wuhua.ui.fragment.LaunchFragment;
import com.atwork.wuhua.ui.fragment.MyFragment;
import com.atwork.wuhua.utils.GlideUtils.GlideUtil;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.UserUtil;
import com.atwork.wuhua.wrideg.NoScrollViewPage;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    @BindView(R.id.vp_main)
    NoScrollViewPage vpMain;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;
    private MainPageAdapter mainPageAdapter;
    private List<Fragment> fragmentList;
    private HomePageFragment homePageFragment;
    private LaunchFragment launchFragment;
    private CommunityFragment communityFragment;
    private MyFragment myFragment;


    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        homePageFragment = new HomePageFragment();
        launchFragment = new LaunchFragment();
        communityFragment = new CommunityFragment();
        myFragment = new MyFragment();
        fragmentList = new ArrayList<>();
        fragmentList.add(homePageFragment);
        fragmentList.add(launchFragment);
        fragmentList.add(communityFragment);
        fragmentList.add(myFragment);
        mainPageAdapter = new MainPageAdapter(getSupportFragmentManager(), fragmentList);
        vpMain.setAdapter(mainPageAdapter);
        vpMain.setCurrentItem(0);
        vpMain.setOffscreenPageLimit(4);
        vpMain.addOnPageChangeListener(this);
        rgMain.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_map:
                vpMain.setCurrentItem(0, false);
                break;
            case R.id.rb_server:
                vpMain.setCurrentItem(1, false);
                break;
            case R.id.rb_community:
                vpMain.setCurrentItem(2, false);
                break;
            case R.id.rb_my:
                vpMain.setCurrentItem(3, false);
                break;
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        RadioButton radioButton = (RadioButton) rgMain.getChildAt(i);
        radioButton.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    private long mExitTime;


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                showToast("再按一次退出程序");
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                System.exit(0);
            }
            return true;
        }


        return super.onKeyDown(keyCode, event);
    }

}
